package com.ld.common.log;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.Calendar;

import com.ld.shieldsb.dao.MySqlBaseDao;

/**
 * 
 * 项目LOG日志相关
 * 
 * @ClassName DBLogDao
 * @author <a href="mailto:donggongai@126.com" target="_blank">kevin</a>
 * @date 2016年8月25日 下午2:40:18
 *
 */
public class DBLogDao extends MySqlBaseDao {
    String tableName = "log4j_info";

    public DBLogDao() {
        super("db0.");
    }

    /**
     * 
     * 保存解析出的LOG日志
     * 
     * @Title saveOrUpdate
     * @param event
     *            日志主体
     * @param serverIp
     *            ip地址
     * @param MachineName
     *            机器名
     * @param projectDir
     *            存放目录
     * @return
     * @throws Exception
     */
    public boolean saveOrUpdate(String message, String hash, String throwableStrRep, String serverIp, String MachineName, String projectDir)
            throws Exception {

        String sql = "select id from " + tableName + " where hash=? and createTime>=?";
        Calendar c = Calendar.getInstance();
        c.add(Calendar.HOUR, -12);
        Long id = getLongThrowException(sql, hash, new Date(c.getTime().getTime()));// 查询当天
        if (id != null && id != 0) {
            sql = "update " + tableName + " set num=num+1,updateTime=? where id=?";
            return update(sql, new Timestamp(System.currentTimeMillis()), id) > 0;
        } else {
            sql = "insert into " + tableName
                    + "(message,hash,throwableStrRep,createTime,num,serverIp,MachineName,projectDir) values(?,?,?,?,1,?,?,?)";

            return update(sql, message, hash, throwableStrRep, new Timestamp(System.currentTimeMillis()), serverIp, MachineName,
                    projectDir) > 0;
        }
    }

}
